﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Serialization;
using ImportEnt;

namespace WpfImoprt
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnSetJson_Click(object sender, RoutedEventArgs e)
        {
            var singleNode = "" + "King";
            var titleNode = "" + DateTime.Now.Ticks;

            if (!string.IsNullOrEmpty(titleNode) && !string.IsNullOrEmpty(singleNode))
            {
                DiaryEnt ent = new DiaryEnt()
                {
                    Action = "addDiary",
                    Tokin = "1181250294x",
                    DiaryTitle = titleNode,
                    DiaryContent = singleNode,
                    DiaryTag = "系统抓取"
                };

                string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(ent);
                SendMessage(strJson, "http://localhost:32375/Import.ashx");
            }
            else
            {
                MessageBox.Show("文章信息获取出错");
            }

        }

        private void btnSetXml_Click(object sender, RoutedEventArgs e)
        {

            var singleNode = "" + "King";
            var titleNode = "" + DateTime.Now.Ticks;

            if (!string.IsNullOrEmpty(titleNode) && !string.IsNullOrEmpty(singleNode))
            {
                DiarysXml xml = new DiarysXml()
                {
                    Action = "addDiary",
                    Tokin = "1181250294x",


                };

                var list = new List<Diarys>()
                  {
                      new Diarys(){DiaryTitle = "king1", DiaryContent = "Content1"},
                       new Diarys(){DiaryTitle = "king2", DiaryContent = "Content2"}
                  };
                xml.MessageList = list;


                string strXml = Serializer.ObjectToXML(xml);
                SendMessage(strXml, "http://localhost:32375/ImportXml.ashx");



            }
            else
            {
                MessageBox.Show("文章信息获取出错");
            }

        }

        void SendMessage(string strMessage, string postUrl)
        {


            var byts = Encoding.UTF8.GetBytes(strMessage);

            WebClient client = new WebClient();


            client.UploadDataAsync(new Uri(postUrl), byts);
            client.UploadDataCompleted += client_UploadDataCompleted;


        }

        void client_UploadDataCompleted(object sender, UploadDataCompletedEventArgs e)
        {
            if (!e.Cancelled)
            {

                string res = Encoding.UTF8.GetString(e.Result);

                MessageBox.Show(res);
            }
        }


    }
}
